package com.entor.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.entor.dao.TClassRepository;
import com.entor.model.TClass;
import com.entor.service.TCService;
@Service
public class TClassServiceImpl implements TCService{
	
	@Autowired
	private TClassRepository tcr;

	@Override
	public List<TClass> findAll() {
		
		Iterable<TClass> tcs = tcr.findAll();//查询所有
		Iterator<TClass> tc = tcs.iterator();//迭代器
		
		List<TClass> list =new ArrayList<TClass>();
		while(tc.hasNext()) {//指针下移，判断是否还存在下一个值
			list.add(tc.next());
		}
		return list;
	}

	@Override
	public List<TClass> findOne(TClass tc) {
		List<TClass> list = tcr.findByName(tc.getId(), tc.getName());
		return list;
	}

	@Override
	public TClass findById(Integer id) {
		TClass tc = tcr.findOne(id);
		return tc;
	}

	@Override
	public List<TClass> findByName(String name) {
		List<TClass> list = tcr.findByName(name);
		return list;
	}

	
	@Transactional(rollbackFor= {ArithmeticException.class})
	@Override
	public int save(TClass tc) {//包含insert 和 update
		TClass t = null;
		try {
			t = tcr.save(tc);
			int c = 10/0;
			
		} catch (ArithmeticException e) {
			throw new ArithmeticException("除数不为0");
		}
		return t==null?0:1;
	}

	@Transactional
	@Override
	public int delete(Integer id) {
		tcr.delete(id);
		return 0;
	}

}
